package com.footair.dy;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.WindowManager;

import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;


public class PopService extends Service {

    private WindowManager mWindowManager;
    private WebView view;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initWindow();
        return super.onStartCommand(intent, flags, startId);
    }


    //    @androidx.annotation.Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWindowManager.removeView(view);
        mWindowManager = null;
    }

    private void initWindow() {
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
        int screenHeight = mWindowManager.getDefaultDisplay().getHeight();
        //        WebView view = new WebView(getApplicationContext());
        view = new WebView(getApplicationContext());
        //        TextView textView = findViewById(R.id.tv_core);
        WebSettings settings = view.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAllowFileAccess(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);
        settings.setSupportMultipleWindows(true);
        // webSetting.setLoadWithOverviewMode(true);
        settings.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setGeolocationEnabled(true);
        settings.setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        WebViewClient webViewClient = new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return true;
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                String js = "var script = document.createElement('script');";
                js += "script.type = 'text/javascript';";
                js += "var video_douyu=document.getElementsByTagName('video')[0];";
                js += "video_douyu.play();";
                //                js += "video_douyu.muted=true;";
                webView.loadUrl("javascript:" + js);
            }
        };
        view.setWebViewClient(webViewClient);
        view.loadUrl("https://m.douyu.com/78561");


        //        X5WebView view = new X5WebView(getApplicationContext(), null);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        lp.x = screenWidth;
        lp.y = screenHeight;

        lp.type = WindowManager.LayoutParams.TYPE_PHONE;
        lp.format = PixelFormat.RGBA_8888;
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        //        lp.width = screenWidth;
        //        lp.height = screenHeight;
        lp.width = 200;
        lp.height = 200;
        //        lp.width = 0;
        //        lp.height = 0;
        view.setLayoutParams(lp);
        mWindowManager.addView(view, lp);
        //        mAllView.add(view);
    }

}
